package top.z.orm.handle.osql.util;

import top.z.orm.handle.osql.core.Logger;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class MD5 {

    public static String getMd5(String text) {
        byte[]        secretBytes;
        StringBuilder re;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(text.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            Logger.error("Have Exception when generate MD5. " + e.getMessage());
            return null;
        }
        re = new StringBuilder(new BigInteger(1, secretBytes).toString(16));
        for (int i = 0; i < 32 - re.length(); i++) {
            re.insert(0, "0");
        }
        return re.toString();
    }
}